<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JSP与EL与JSTL | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/41.2f80f24b.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>前端技术</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/84c5d2/" class="sidebar-link">HTML与CSS</a></li><li><a href="/pages/42b6b6/" class="sidebar-link">JavaScript与DOM</a></li><li><a href="/pages/1125b7/" class="sidebar-link">网络编程与Tomcat</a></li><li><a href="/pages/be0e03/" class="sidebar-link">Http与Servlet</a></li><li><a href="/pages/9902e4/" class="sidebar-link">请求与响应</a></li><li><a href="/pages/18345f/" aria-current="page" class="active sidebar-link">JSP与EL与JSTL</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/18345f/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/18345f/#二、动态网页jsp" class="sidebar-link">二、动态网页JSP</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_2-1-jsp-概述-理解" class="sidebar-link">2.1 JSP 概述（理解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-1-1-什么是-jsp" class="sidebar-link">2.1.1 什么是 JSP</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-1-2-jsp-的优势" class="sidebar-link">2.1.2 JSP 的优势</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-1-3-jsp-初体验" class="sidebar-link">2.1.3 JSP 初体验</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_2-2-jsp-运行过程-了解" class="sidebar-link">2.2 JSP 运行过程（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_2-3-jsp常见问题-了解" class="sidebar-link">2.3 JSP常见问题（了解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-3-1-jsp-翻译的-servlet-位置在哪里" class="sidebar-link">2.3.1 JSP 翻译的 Servlet 位置在哪里？</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-3-2-jsp-和-servlet-是什么关系" class="sidebar-link">2.3.2 JSP 和 Servlet 是什么关系？</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-3-3-jsp-什么时候翻译成-servlet-翻译多少次" class="sidebar-link">2.3.3 JSP 什么时候翻译成 Servlet？翻译多少次？</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_2-4-jsp-基本语法-了解" class="sidebar-link">2.4 JSP 基本语法（了解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-4-1-jsp-的注释" class="sidebar-link">2.4.1 JSP 的注释</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-4-2-jsp-脚本片段" class="sidebar-link">2.4.2 JSP 脚本片段</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-4-3-jsp-脚本表达式" class="sidebar-link">2.4.3 JSP 脚本表达式</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-4-4-jsp-声明" class="sidebar-link">2.4.4 JSP 声明</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_2-4-5-jsp-页面三种脚本小结" class="sidebar-link">2.4.5 JSP 页面三种脚本小结</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/18345f/#三、转发和重定向-掌握" class="sidebar-link">三、转发和重定向（掌握）</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_3-1-转发和重定向的作用-掌握" class="sidebar-link">3.1 转发和重定向的作用(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_3-2-转发概述-理解" class="sidebar-link">3.2 转发概述（理解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-2-1-跳转位置" class="sidebar-link">3.2.1 跳转位置</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-2-2-转发原理" class="sidebar-link">3.2.2 转发原理</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-2-3-转发方法-掌握" class="sidebar-link">3.2.3 转发方法(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_3-3-重定向概述-理解" class="sidebar-link">3.3 重定向概述（理解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-3-1-跳转位置" class="sidebar-link">3.3.1 跳转位置</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-3-2-重定向原理" class="sidebar-link">3.3.2 重定向原理</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-3-2-重定向方法-掌握" class="sidebar-link">3.3.2 重定向方法(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_3-4-转发和重定向代码" class="sidebar-link">3.4 转发和重定向代码</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-4-1-需求说明" class="sidebar-link">3.4.1 需求说明</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-4-2-oneservlet-代码" class="sidebar-link">3.4.2 OneServlet 代码</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_3-4-3-twoservlet代码" class="sidebar-link">3.4.3 TwoServlet代码</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_3-5-转发和重定向区别-掌握" class="sidebar-link">3.5 转发和重定向区别(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_3-6-常见面试题-掌握" class="sidebar-link">3.6 常见面试题(掌握)</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/18345f/#四、三大作用域" class="sidebar-link">四、三大作用域</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_4-1-什么是作用域-掌握" class="sidebar-link">4.1 什么是作用域(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_4-2-作用域类型-掌握" class="sidebar-link">4.2 作用域类型(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_4-3-作用域方法-掌握" class="sidebar-link">4.3 作用域方法(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_4-4-作用域操作-理解" class="sidebar-link">4.4 作用域操作（理解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_4-4-1-需求" class="sidebar-link">4.4.1 需求</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_4-4-2-firstservlet代码" class="sidebar-link">4.4.2 FirstServlet代码</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_4-4-3-secondservlet代码" class="sidebar-link">4.4.3 SecondServlet代码</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_4-5-如何选择作用域" class="sidebar-link">4.5 如何选择作用域</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/18345f/#五、el表达式" class="sidebar-link">五、EL表达式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_5-1-el-表达式概述-理解" class="sidebar-link">5.1 EL 表达式概述（理解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_5-2-el-表达式获取不同类型数据-掌握" class="sidebar-link">5.2 EL 表达式获取不同类型数据（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-2-1-示例1" class="sidebar-link">5.2.1 示例1</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-2-2-注意事项" class="sidebar-link">5.2.2 注意事项</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-2-3-示例2" class="sidebar-link">5.2.3 示例2</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_5-3-el-中使用表达式" class="sidebar-link">5.3 EL 中使用表达式</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-3-1-算术表达式" class="sidebar-link">5.3.1 算术表达式</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-3-2-比较表达式" class="sidebar-link">5.3.2 比较表达式</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-3-3-逻辑表达式" class="sidebar-link">5.3.3 逻辑表达式</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-3-4-三元运算" class="sidebar-link">5.3.4 三元运算</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-3-5-判空表达式" class="sidebar-link">5.3.5 判空表达式</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-3-6-示例代码-使用上面所有的运算符" class="sidebar-link">5.3.6 示例代码：使用上面所有的运算符</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_5-4-el-从四个作用域中取值-掌握" class="sidebar-link">5.4 EL 从四个作用域中取值（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-4-1-pagecontext-对象" class="sidebar-link">5.4.1 PageContext 对象</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_5-4-2-el-指定域获取数据" class="sidebar-link">5.4.2 EL 指定域获取数据</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/18345f/#六、jstl-标签库" class="sidebar-link">六、JSTL 标签库</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_6-1-jstl-概述-理解" class="sidebar-link">6.1 JSTL 概述（理解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-1-1-为什么使用-jstl" class="sidebar-link">6.1.1 为什么使用 JSTL</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-1-2-什么是-jstl" class="sidebar-link">6.1.2 什么是 JSTL</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-1-3-jstl-常用标签库" class="sidebar-link">6.1.3 JSTL 常用标签库</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-1-4-jstl-使用步骤" class="sidebar-link">6.1.4 JSTL 使用步骤</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_6-2-核心标签库常用标签" class="sidebar-link">6.2 核心标签库常用标签</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-2-1-c-if-标签-掌握" class="sidebar-link">6.2.1 &lt;c:if&gt; 标签（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-2-2-c-choose-标签-掌握" class="sidebar-link">6.2.2 &lt;c:choose&gt;标签（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-2-3-c-foreach-标签-掌握" class="sidebar-link">6.2.3 &lt;c:forEach&gt;标签（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_6-3-格式化标签库常用标签" class="sidebar-link">6.3 格式化标签库常用标签</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_6-3-1-fmt-formatdate" class="sidebar-link">6.3.1 &lt;fmt:formatDate&gt;</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/18345f/#七、小结与作业" class="sidebar-link">七、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_7-1-小结" class="sidebar-link">7.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/18345f/#_7-2-作业" class="sidebar-link">7. 2 作业</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_7-2-1-题目1" class="sidebar-link">7.2.1 题目1</a></li><li class="sidebar-sub-header level4"><a href="/pages/18345f/#_7-2-2-题目2" class="sidebar-link">7.2.2 题目2</a></li></ul></li></ul></li><li><a href="/pages/aa67fd/" class="sidebar-link">WEB版CRUD</a></li><li><a href="/pages/baec2f/" class="sidebar-link">Cookie与Session</a></li><li><a href="/pages/73e2cb/" class="sidebar-link">文件上传与下载和登录</a></li><li><a href="/pages/f99e33/" class="sidebar-link">项目实战-MIS系统</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF" title="分类" data-v-06225672>前端技术</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-03</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">JSP与EL与JSTL<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="jsp与el与jstl"><a href="#jsp与el与jstl" class="header-anchor">#</a> JSP与EL与JSTL</h1> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <ul><li><p>能够说出 jsp 的优势</p></li> <li><p>了解 jsp 脚本片段、声明、脚本表达式</p></li> <li><p>能够书写 jsp 页面来写标签即可</p></li> <li><p>能够说明为什么需要做组件的跳转和数据的共享</p></li> <li><p>理解转发和重定向两种跳转方式的区别/特点</p></li> <li><p>能够使用转发和重定向实现页面跳转</p></li> <li><p>能够往三大作用域中存取数据</p></li> <li><p>能够说出 EL 表达式的作用并能够使用 EL 表达式</p></li> <li><p>能够说出 JSTL 标签库的作用</p></li> <li><p>能够使用 JSTL 标签库的 foreach if  choose 标签</p></li></ul> <h2 id="二、动态网页jsp"><a href="#二、动态网页jsp" class="header-anchor">#</a> 二、动态网页JSP</h2> <h3 id="_2-1-jsp-概述-理解"><a href="#_2-1-jsp-概述-理解" class="header-anchor">#</a> 2.1 JSP 概述（理解）</h3> <h4 id="_2-1-1-什么是-jsp"><a href="#_2-1-1-什么是-jsp" class="header-anchor">#</a> 2.1.1 什么是 JSP</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424092104059-1709357681598.png" alt="image-20200424092104059"></p> <p>​	<strong>JSP——Java Server Page运行在服务器端的 Java 页面，以前学习的 HTML 运行在浏览器端。JSP 是运行在服务器上，最终还是解析成静态的HTML，运行在浏览器。我们在浏览器上看到的HTML页面，其实是 JSP 在服务器上运行的结果。</strong></p> <h4 id="_2-1-2-jsp-的优势"><a href="#_2-1-2-jsp-的优势" class="header-anchor">#</a> 2.1.2 JSP 的优势</h4> <table><thead><tr><th>技术</th> <th>特点</th></tr></thead> <tbody><tr><td>HTML</td> <td>静态页面  优点：方便页面美化操作，写JS脚本和CSS代码都比较方便。  <br>缺点：不能写动态的内容。</td></tr> <tr><td>Servlet</td> <td>运行在服务器上小 Java 程序 <br>优点：制作动态内容  <br>缺点：不方便写 HTML 以及 CSS 和 JS 代码</td></tr> <tr><td>JSP</td> <td>JSP = HTML + Servlet  <br>特点：既有 HTML 的优点：方便美化和编写 JS 代码。 <br>又有 Servlet 优点：可以在页面写 Java 代码（不建议），可以制作动态内容。</td></tr></tbody></table> <h4 id="_2-1-3-jsp-初体验"><a href="#_2-1-3-jsp-初体验" class="header-anchor">#</a> 2.1.3 JSP 初体验</h4> <ul><li>需求：在浏览器上输出服务器当前的时间</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200501074853448.png" alt="image-20200501074853448"></p> <div class="language-HTML line-numbers-mode"><pre class="language-html"><code>&lt;%--导入包--%&gt;
&lt;%@ page import=&quot;java.util.Date&quot; %&gt;
&lt;%--jsp的page指令:  指定页面内容的类型，编码。页面使用编程语言：Java  --%&gt;
&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>显示时间<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
   &lt;%=new Date()%&gt;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_2-2-jsp-运行过程-了解"><a href="#_2-2-jsp-运行过程-了解" class="header-anchor">#</a> 2.2 JSP 运行过程（了解）</h3> <p>​	JspServlet 首先会将 jsp 文件代码翻译成 Servlet 的 Java 代码，然后再编译成字节码文件执行,其底层就是一个 Servlet。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424093408316.png" alt="image-20200424093408316"></p> <ol><li><p>浏览器请求 JSP 页面，Tomcat 中的 JspServlet 会先 JSP 文件翻译成 Servlet 文件：xxx_jsp.java</p></li> <li><p>将 xxx_jsp.java 文件编译产生字节码文件：xxx_jsp.class</p></li> <li><p>加载字节码文件 xxx_jsp.class 并创建 xxx_jsp 对象</p></li> <li><p>调用 xxx_jsp 的 service 方法处理请求并响应数据</p></li></ol> <h3 id="_2-3-jsp常见问题-了解"><a href="#_2-3-jsp常见问题-了解" class="header-anchor">#</a> 2.3 JSP常见问题（了解）</h3> <h4 id="_2-3-1-jsp-翻译的-servlet-位置在哪里"><a href="#_2-3-1-jsp-翻译的-servlet-位置在哪里" class="header-anchor">#</a> 2.3.1 JSP 翻译的 Servlet 位置在哪里？</h4> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code>c:\Users\Administrator\.IntelliJIdea2019.3\system\tomcat\tomcat版本_项目名\work\Catalina
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424095049103.png" alt="image-20200424095049103"></p> <h4 id="_2-3-2-jsp-和-servlet-是什么关系"><a href="#_2-3-2-jsp-和-servlet-是什么关系" class="header-anchor">#</a> 2.3.2 JSP 和 Servlet 是什么关系？</h4> <ol><li>查看源码：</li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> hello_jsp <span class="token keyword">extends</span> <span class="token class-name"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>jasper<span class="token punctuation">.</span>runtime<span class="token punctuation">.</span></span>HttpJspBase</span>
<span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">HttpJspBase</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ol start="2"><li>结论：JSP 底层就是 Servlet</li></ol> <h4 id="_2-3-3-jsp-什么时候翻译成-servlet-翻译多少次"><a href="#_2-3-3-jsp-什么时候翻译成-servlet-翻译多少次" class="header-anchor">#</a> 2.3.3 JSP 什么时候翻译成 Servlet？翻译多少次？</h4> <p>​	浏览器第一次访问 JSP 的时候，由 Tomcat 将 JSP 翻译成了 Servlet，并且编译成字节码文件，只生成1次。如果这期间修改了JSP内容，就会重新翻译。</p> <h3 id="_2-4-jsp-基本语法-了解"><a href="#_2-4-jsp-基本语法-了解" class="header-anchor">#</a> 2.4 JSP 基本语法（了解）</h3> <h4 id="_2-4-1-jsp-的注释"><a href="#_2-4-1-jsp-的注释" class="header-anchor">#</a> 2.4.1 JSP 的注释</h4> <table><thead><tr><th>JSP 注释语法</th> <th>说明</th></tr></thead> <tbody><tr><td>&lt;%-- 注释内容 --%&gt;</td> <td>这是注释，不会翻译到Servelt 中<br>在翻译到java文件的时候,会忽略该注释的内容.</td></tr></tbody></table> <h4 id="_2-4-2-jsp-脚本片段"><a href="#_2-4-2-jsp-脚本片段" class="header-anchor">#</a> 2.4.2 JSP 脚本片段</h4> <table><thead><tr><th>JSP 脚本片段</th> <th>说明</th></tr></thead> <tbody><tr><td>语法</td> <td>&lt;% 一行或多行 Java 代码;   %&gt;</td></tr> <tr><td>作用</td> <td>在 JSP 的页面上执行一段 Java 代码，必须符合 Java 的语法规则。<br>每行代码以分号结束。  <br>可以在脚本片段中使用 Java 的单行或多行注释语法<br>代码被翻译到 service 方法中</td></tr></tbody></table> <h4 id="_2-4-3-jsp-脚本表达式"><a href="#_2-4-3-jsp-脚本表达式" class="header-anchor">#</a> 2.4.3 JSP 脚本表达式</h4> <table><thead><tr><th>JSP脚本表达式</th> <th>说明</th></tr></thead> <tbody><tr><td>语法</td> <td>&lt;%= 变量名或表达式%&gt;  <br>翻译成 Servlet 变成 out.print() 打印输出  <br>注意：后面没有分号</td></tr> <tr><td>作用</td> <td>直接输出变量的值或对表达式进行计算</td></tr></tbody></table> <ul><li><strong>JSP 代码演示</strong></li></ul> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code>JSP脚本表达式：
&lt;%
    String name = &quot;孙悟空&quot;;
%&gt;
&lt;%=name%&gt; &lt;br&gt;
&lt;%=3*4%&gt;  &lt;br&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li><strong>在 Servlet 中生成</strong></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot;JSP脚本表达式：\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token class-name">String</span> name <span class="token operator">=</span> <span class="token string">&quot;孙悟空&quot;</span><span class="token punctuation">;</span>

out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token char">'\r'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token char">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot; &lt;br&gt;\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">*</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;br&gt;\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="_2-4-4-jsp-声明"><a href="#_2-4-4-jsp-声明" class="header-anchor">#</a> 2.4.4 JSP 声明</h4> <table><thead><tr><th>JSP声明</th> <th>说明</th></tr></thead> <tbody><tr><td>语法</td> <td>&lt;%!   全局变量或方法  %&gt;</td></tr> <tr><td>作用</td> <td>用于在JSP页面上声明全局变量或方法  <br>注：很少在页面上声明方法</td></tr></tbody></table> <ul><li><p><strong>JSP 代码演示</strong></p> <ol><li><p>在脚本片段中创建一个字符串，使用脚本表达式输出</p></li> <li><p>在声明中也创建一个同名的字符，使用脚本表达式输出，会不会有问题？</p></li></ol></li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>JSP声明<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
&lt;%--JSP脚本片段--%&gt;
&lt;%
    String name = &quot;jack&quot;;
%&gt;
&lt;%--JSP声明--%&gt;
&lt;%!
    String name=&quot;rose&quot;;
%&gt;
&lt;%=name%&gt;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
&lt;%=this.name%&gt;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li><strong>效果如下</strong></li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221194843065.png" alt="image-20240221194843065"></p> <ul><li><strong>生成的Servlet代码</strong></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">String</span> name <span class="token operator">=</span> <span class="token string">&quot;jack&quot;</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// jack定义在方法中</span>
out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;br&gt;\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//rose是声明的一个成员变量，出现在类中，而不是在方法中。</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li><p><strong>结论</strong></p> <p>JSP 声明中声明变量是在类中的成员变量。而在 JSP 脚本片段中声明的变量是局部变量，声明在_jspService()方法中。</p></li></ul> <h4 id="_2-4-5-jsp-页面三种脚本小结"><a href="#_2-4-5-jsp-页面三种脚本小结" class="header-anchor">#</a> 2.4.5 JSP 页面三种脚本小结</h4> <table><thead><tr><th>JSP三种脚本</th> <th>功能</th> <th>语法</th></tr></thead> <tbody><tr><td>JSP脚本片段</td> <td>在 JSP 中执行Java代码</td> <td>&lt;% Java代码; %&gt;</td></tr> <tr><td>JSP脚本表达式</td> <td>在JSP中输出变量值或表达式结果</td> <td>&lt;%= 变量或表达式%&gt;</td></tr> <tr><td>JSP声明</td> <td>声明全局变量或方法</td> <td>&lt;%! 声明 %&gt;</td></tr></tbody></table> <h2 id="三、转发和重定向-掌握"><a href="#三、转发和重定向-掌握" class="header-anchor">#</a> 三、转发和重定向（掌握）</h2> <h3 id="_3-1-转发和重定向的作用-掌握"><a href="#_3-1-转发和重定向的作用-掌握" class="header-anchor">#</a> 3.1 转发和重定向的作用(掌握)</h3> <p>用于 web 组件的跳转，从A 组件跳到另一个组件</p> <p>目前学到的 web 组件 : JSP, Servlet</p> <h3 id="_3-2-转发概述-理解"><a href="#_3-2-转发概述-理解" class="header-anchor">#</a> 3.2 转发概述（理解）</h3> <h4 id="_3-2-1-跳转位置"><a href="#_3-2-1-跳转位置" class="header-anchor">#</a> 3.2.1 跳转位置</h4> <ul><li>在服务器端进行的组件(资源)的跳转</li></ul> <h4 id="_3-2-2-转发原理"><a href="#_3-2-2-转发原理" class="header-anchor">#</a> 3.2.2 转发原理</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424101847738.png" alt="image-20200424101811806"></p> <h4 id="_3-2-3-转发方法-掌握"><a href="#_3-2-3-转发方法-掌握" class="header-anchor">#</a> 3.2.3 转发方法(掌握)</h4> <table><thead><tr><th>request 对象与转发相关方法</th></tr></thead> <tbody><tr><td>request.getRequestDispatcher(&quot;/要跳转到的地址&quot;)<strong>.forward(request, response);</strong></td></tr></tbody></table> <h3 id="_3-3-重定向概述-理解"><a href="#_3-3-重定向概述-理解" class="header-anchor">#</a> 3.3 重定向概述（理解）</h3> <h4 id="_3-3-1-跳转位置"><a href="#_3-3-1-跳转位置" class="header-anchor">#</a> 3.3.1 跳转位置</h4> <ul><li>在浏览器端进行的页面(组件/资源)跳转</li></ul> <h4 id="_3-3-2-重定向原理"><a href="#_3-3-2-重定向原理" class="header-anchor">#</a> 3.3.2 重定向原理</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424102524457.png" alt="image-20200424102524457"></p> <h4 id="_3-3-2-重定向方法-掌握"><a href="#_3-3-2-重定向方法-掌握" class="header-anchor">#</a> 3.3.2 重定向方法(掌握)</h4> <table><thead><tr><th>response对象与重定向相关方法</th></tr></thead> <tbody><tr><td>response.sendRedirect(&quot;要跳转的地址&quot;)</td></tr></tbody></table> <h3 id="_3-4-转发和重定向代码"><a href="#_3-4-转发和重定向代码" class="header-anchor">#</a> 3.4 转发和重定向代码</h3> <h4 id="_3-4-1-需求说明"><a href="#_3-4-1-需求说明" class="header-anchor">#</a> 3.4.1 需求说明</h4> <p>创建 OneServlet 和 TwoServlet，实现浏览器访问 OneServlet，使用转发或重定向跳转到 TwoServlet</p> <h4 id="_3-4-2-oneservlet-代码"><a href="#_3-4-2-oneservlet-代码" class="header-anchor">#</a> 3.4.2 OneServlet 代码</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/one&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OneServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span> username <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;username&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span>
      
        <span class="token comment">// 转发到 twoServlet</span>
        req<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;/forward/two&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span>resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 可以跳转到 WEB-INF 中</span>
        <span class="token comment">// req.getRequestDispatcher(&quot;/WEB-INF/hello.jsp&quot;).forward(req,resp);</span>
        <span class="token comment">// 不能访问到别人的项目(外域)</span>
        <span class="token comment">//req.getRequestDispatcher(&quot;http://www.baidu.com&quot;).forward(req,resp);</span>
        
        
        <span class="token comment">// 重定向到 twoServlet</span>
        <span class="token comment">// resp.sendRedirect(&quot;/day10/redirect/two&quot;);</span>
        <span class="token comment">// 不可以访问 WEB-INF 下的资源</span>
        <span class="token comment">// resp.sendRedirect(&quot;/day10/WEB-INF/hello.jsp&quot;);</span>
        <span class="token comment">// 可以访问其他项目(外域)</span>
        <span class="token comment">//resp.sendRedirect(&quot;http://www.baidu.com&quot;);</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;后面的代码可以执行到,但是没有意义,以后不会在跳转之后写代码&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h4 id="_3-4-3-twoservlet代码"><a href="#_3-4-3-twoservlet代码" class="header-anchor">#</a> 3.4.3 TwoServlet代码</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">PrintWriter</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span>urlPatterns <span class="token operator">=</span> <span class="token string">&quot;/two&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TwoServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
         <span class="token comment">// 设置内容类型和编码</span>
         resp<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;text/html;charset=utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token comment">// 获得字符打印流</span>
         <span class="token class-name">PrintWriter</span> out <span class="token operator">=</span> resp<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;我是TwoServlet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><ul><li>浏览器访问 OneServlet 的效果：</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424104644029.png" alt="image-20200424104644029"></p> <h3 id="_3-5-转发和重定向区别-掌握"><a href="#_3-5-转发和重定向区别-掌握" class="header-anchor">#</a> 3.5 转发和重定向区别(掌握)</h3> <table><thead><tr><th>区别</th> <th style="text-align:left;">转发 forward</th> <th>重定向 redirect</th></tr></thead> <tbody><tr><td>项目路径path</td> <td style="text-align:left;">服务端的根目录：<br><code>http://localhost:8080/项目地址/</code><br>跳转的 path 不用写项目路径(虚拟目录)</td> <td>浏览器的根目录(端口之后):<br><code>http://localhost:8080</code><br>跳转 path 需加上项目路径(虚拟目录)</td></tr> <tr><td>地址栏</td> <td style="text-align:left;">地址栏不会发生变化，还是上一个地址</td> <td>会变化，显示新的地址</td></tr> <tr><td>跳转位置</td> <td style="text-align:left;">在 <strong>服务端</strong> 进行跳转</td> <td>在 <strong>浏览器端</strong> 跳转</td></tr> <tr><td>请求对象(域)</td> <td style="text-align:left;">请求域数据不会丢失，因为是同一个请求</td> <td>请求域数据会丢失，因为不是同一个请求</td></tr> <tr><td>WEB-INF下资源</td> <td style="text-align:left;">请求转发可以跳转到 WEB-INF 下的资源</td> <td>重定向不可以跳转到 WEB-INF 下的资源</td></tr> <tr><td>跨域访问</td> <td style="text-align:left;">请求转发不可以跳转到其他域名或项目下的资源<br>（例：http://www.baidu.com）</td> <td>重定向可以跳转到其他域名或项目下的资源<br>（例：http://www.baidu.com）</td></tr></tbody></table> <h3 id="_3-6-常见面试题-掌握"><a href="#_3-6-常见面试题-掌握" class="header-anchor">#</a> 3.6 常见面试题(掌握)</h3> <ul><li>[x] 什么时候使用转发，什么时候使用重定向？</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>如果需要保留请求域中的数据，使用转发
如果要跳转到 WEB-INF 目录中的页面（资源），使用请求转发
如果要访问其他项目(跨域)，使用重定向
其他的任选
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>[x] 转发或重定向后续的代码是否还会运行？</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>无论是重定向还是转发，后续代码都会执行，但一般转发或重定向后面不会有代码，没有执行的意义了，因为不管是转发还是重定向浏览器最终显示的都是跳转后页面的数据
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="四、三大作用域"><a href="#四、三大作用域" class="header-anchor">#</a> 四、三大作用域</h2> <h3 id="_4-1-什么是作用域-掌握"><a href="#_4-1-什么是作用域-掌握" class="header-anchor">#</a> 4.1 什么是作用域(掌握)</h3> <p>用于 Servlet 之间数据共享的服务器内存区域，作用域结构是一个 Map&lt;String, Object&gt;</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/img01.png" alt=""></p> <h3 id="_4-2-作用域类型-掌握"><a href="#_4-2-作用域类型-掌握" class="header-anchor">#</a> 4.2 作用域类型(掌握)</h3> <table><thead><tr><th>作用域</th> <th>类型</th> <th>作用范围</th></tr></thead> <tbody><tr><td>请求域</td> <td>HttpServletRequest</td> <td>只在同一次请求中起作用</td></tr> <tr><td>会话域</td> <td>HttpSession</td> <td>同一个会话中起作用<br>浏览器第1次访问服务器的资源直到浏览器关闭<br>的整个过程称为1次会话</td></tr> <tr><td>上下文域</td> <td>ServletContext</td> <td>同一个应用中起作用<br>服务器启动直到服务器关闭的整个过程都起作用</td></tr></tbody></table> <h3 id="_4-3-作用域方法-掌握"><a href="#_4-3-作用域方法-掌握" class="header-anchor">#</a> 4.3 作用域方法(掌握)</h3> <table><thead><tr><th>与作用域有关的方法</th> <th>作用</th></tr></thead> <tbody><tr><td>Object getAttribute(&quot;键&quot;)</td> <td>从中得到一个值</td></tr> <tr><td>void setAttribute(&quot;键&quot;,Object数据)</td> <td>向作用域中存储键值对数据</td></tr> <tr><td>void removeAttribute(&quot;键&quot;)</td> <td>删除作用域中的键值对数据</td></tr></tbody></table> <ul><li>哪个作用域对象调用方法就操作对应的作用域数据。</li></ul> <h3 id="_4-4-作用域操作-理解"><a href="#_4-4-作用域操作-理解" class="header-anchor">#</a> 4.4 作用域操作（理解）</h3> <h4 id="_4-4-1-需求"><a href="#_4-4-1-需求" class="header-anchor">#</a> 4.4.1 需求</h4> <p>在 FirstServlet 存储键值到作用域中，转发到另一个 SecondServlet，从 SecondServlet 中取出键值并且输出。</p> <h4 id="_4-4-2-firstservlet代码"><a href="#_4-4-2-firstservlet代码" class="header-anchor">#</a> 4.4.2 FirstServlet代码</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/scope/first&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FirstServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
   
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 往请求域存储键值对</span>
        req<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;request&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 往会话域存储键值对</span>
        req<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;session&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 往上下文域存储键值对</span>
        req<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;servletContext&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 转发到second</span>
        req<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;/scope/second&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h4 id="_4-4-3-secondservlet代码"><a href="#_4-4-3-secondservlet代码" class="header-anchor">#</a> 4.4.3 SecondServlet代码</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">PrintWriter</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/scope/second&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SecondServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
         <span class="token comment">// 设置内容类型和编码</span>
         resp<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;text/html;charset=utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token comment">// 获得字符打印流</span>
         <span class="token class-name">PrintWriter</span> out <span class="token operator">=</span> resp<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

         <span class="token comment">// 从请求域中获取数据</span>
         <span class="token class-name">Object</span> reqName <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token comment">// 从会话域中获取数据</span>
         <span class="token class-name">Object</span> sessionName <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token comment">// 从上下文域中获取数据</span>
         <span class="token class-name">Object</span> appName <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

         out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;请求域数据：&quot;</span> <span class="token operator">+</span> reqName <span class="token operator">+</span> <span class="token string">&quot;&lt;br&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;会话域数据：&quot;</span> <span class="token operator">+</span> sessionName <span class="token operator">+</span> <span class="token string">&quot;&lt;br&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;上下文域数据：&quot;</span> <span class="token operator">+</span> appName <span class="token operator">+</span> <span class="token string">&quot;&lt;br&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><ul><li>浏览器访问FirstServlet效果：</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424111429373.png" alt="image-20200424111429373"></p> <p><strong>作用域范围演示案例</strong>（理解）</p> <p>需求: 从 ScopeServlet 中获取三个作用域中的数据 num, 如果获取到则加1再存入,如果获取不到则存入 num 的值为1,最后跳转到 ResultServelt 中来做数据的获取和显示.</p> <p>ScopeServlet.java</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/scope&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ScopeServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 在 ScopeServlet 中获取三个作用域中的数据 num, 如果获取到则加1再存入,</span>
        <span class="token comment">// 如果获取不到则存入 num 的值为1,跳转到 ResultServelt 中来做数据的获取和显示</span>
        <span class="token class-name">Object</span> strNum <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>strNum <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            req<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
            <span class="token class-name">Integer</span> num <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span><span class="token punctuation">)</span>strNum<span class="token punctuation">;</span>
            req<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">,</span>num <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// session作用域</span>
        <span class="token class-name">HttpSession</span> session <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> sessinoNum <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>sessinoNum <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            session<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
            <span class="token class-name">Integer</span> num <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span><span class="token punctuation">)</span>sessinoNum<span class="token punctuation">;</span>
            session<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">,</span>num <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// servletContext 作用域</span>
        <span class="token class-name">ServletContext</span> context <span class="token operator">=</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> ctNum <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>ctNum <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            context<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
            <span class="token class-name">Integer</span> num <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span><span class="token punctuation">)</span>ctNum<span class="token punctuation">;</span>
            context<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">,</span>num <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 跳转</span>
        req<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;/result&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span>resp<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><p>ResultServlet.java</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/result&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ResultServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        resp<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;text/html;charset=utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">PrintWriter</span> out <span class="token operator">=</span> resp<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Object</span> reqNum <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> sessionNum <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> ctNum <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;num&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;br/&gt; request:&quot;</span> <span class="token operator">+</span> reqNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
        out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;br/&gt; session:&quot;</span> <span class="token operator">+</span> sessionNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
        out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;br/&gt; servletContext:&quot;</span> <span class="token operator">+</span> ctNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_4-5-如何选择作用域"><a href="#_4-5-如何选择作用域" class="header-anchor">#</a> 4.5 如何选择作用域</h3> <ul><li>先考虑作用范围小的作用域，如果小作用范围的作用域能满足需求就使用小作用范围的作用域。</li> <li>考虑顺序：请求域--&gt;会话域--&gt;上下文域</li></ul> <h2 id="五、el表达式"><a href="#五、el表达式" class="header-anchor">#</a> 五、EL表达式</h2> <h3 id="_5-1-el-表达式概述-理解"><a href="#_5-1-el-表达式概述-理解" class="header-anchor">#</a> 5.1 EL 表达式概述（理解）</h3> <p><strong>EL 表达式</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200514071237299.png" alt="image-20200514071237299"></p> <ul><li>用于替换 JSP 页面中的脚本表达式，让 JSP 的代码更加简化。</li></ul> <table><thead><tr><th>EL概念</th> <th>EL语法</th> <th>EL作用</th></tr></thead> <tbody><tr><td>英文名称：Expression Language<br>中文名称：表达式语言</td> <td><strong>${</strong> 变量名或表达式**}**</td> <td>1.用于输出<strong>作用域中</strong> 属性值<br>2.用于各种运算：算术，逻辑，<br>关系，三元运算等</td></tr></tbody></table> <ul><li>EL 与 JSP 脚本表达式的区别</li></ul> <table><thead><tr><th>区别</th> <th>JSP脚本表达式</th> <th>EL</th></tr></thead> <tbody><tr><td>语法</td> <td>&lt;%=m%&gt;</td> <td>${m}</td></tr> <tr><td>输出哪里的值</td> <td>输出的是脚本变量  <br>&lt;% int m= 5; %&gt;</td> <td>输出的是作用域中的值  <br>&lt;% request.setAttribute(&quot;m&quot;,  5); %&gt;</td></tr></tbody></table> <ul><li>案例：分别输出脚本变量和作用域中变量值</li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code>&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
&lt;%
    int m=5;
    // 向请求域中添加一个变量
    request.setAttribute(&quot;num&quot;, 6);
%&gt;
输出m:
&lt;%=m%&gt;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
输出作用域中值：
&lt;%= request.getAttribute(&quot;num&quot;)%&gt;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>

使用 EL 表达式输出：
${num}

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_5-2-el-表达式获取不同类型数据-掌握"><a href="#_5-2-el-表达式获取不同类型数据-掌握" class="header-anchor">#</a> 5.2 EL 表达式获取不同类型数据（掌握）</h3> <blockquote><p>从 EL 中取值的前提：数据必须放在作用域中。</p></blockquote> <h4 id="_5-2-1-示例1"><a href="#_5-2-1-示例1" class="header-anchor">#</a> 5.2.1 示例1</h4> <p><strong>执行效果</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220914163431072.png" alt="image-20220914163431072"></p> <p><strong>示例1代码</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@ToString</span>
<span class="token annotation punctuation">@Getter</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Employee</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> id <span class="token operator">=</span> <span class="token number">1L</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name <span class="token operator">=</span> <span class="token string">&quot;小狼&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> hobby <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&quot;Java&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Python&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Dance&quot;</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">&quot;沉鱼&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;落雁&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;闭月&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;羞花&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;key&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;value&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;host&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;wolfcode.cn&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;wolfcode.cn&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;host&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/el2&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">EL2Servlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 从数据库中来获取数据（模拟数据），然后跳转到jsp 来显示</span>
        <span class="token class-name">Employee</span> emp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Employee</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        req<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;emp&quot;</span><span class="token punctuation">,</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        req<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;/el/el2.jsp&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span>resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>el2.jsp</p> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code>&lt;h3&gt;获取 JavaBean 属性&lt;/h3&gt;
name: ${emp.name} --&gt; ${emp.getName()} --&gt; ${emp[&quot;name&quot;]}
&lt;h3&gt;获取数组元素值&lt;/h3&gt;
hobby:${emp.hobby[0]} ${emp.hobby[1]} ${emp.hobby[2]}
&lt;h3&gt;获取集合元素值&lt;/h3&gt;
list:${emp.list[1]} ${emp.list[2]} ${emp.list[3]}
&lt;h3&gt;获取map的元素值&lt;/h3&gt;
map:${emp.map}
&lt;br/&gt;map.key:${emp.map.key}
&lt;br/&gt;map.host:${emp.map.host}
&lt;br/&gt;map.wolfcode.cn:${emp.map[&quot;wolfcode.cn&quot;]}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h4 id="_5-2-2-注意事项"><a href="#_5-2-2-注意事项" class="header-anchor">#</a> 5.2.2 注意事项</h4> <ul><li>JavaBean 的属性名或 map 的键名中如果有特殊字符的写法</li></ul> <table><thead><tr><th>变量名[&quot;键名&quot;]</th></tr></thead> <tbody><tr><td>可以使用双引号或单引号，如：${map[&quot;no3-no4&quot;]}</td></tr></tbody></table> <h4 id="_5-2-3-示例2"><a href="#_5-2-3-示例2" class="header-anchor">#</a> 5.2.3 示例2</h4> <p><strong>执行效果</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424114205824.png" alt="image-20210819194754790"></p> <p><strong>示例2代码</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>domain</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Getter</span>
<span class="token annotation punctuation">@Setter</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">BigDecimal</span> salary<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-jsp line-numbers-mode"><pre class="language-text"><code>&lt;%@ page import=&quot;cn.wolfcode.domain.Person&quot; %&gt;
&lt;%@ page import=&quot;java.util.Arrays&quot; %&gt;
&lt;%@ page import=&quot;java.util.List&quot; %&gt;
&lt;%@ page import=&quot;java.util.HashMap&quot; %&gt;
&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;EL得到不同类型的值&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;%
        // 将员工对象放在页面域
        Person p = new Person(&quot;西施&quot;, new BigDecimal(&quot;10000&quot;));
        // 放到页面域中
        request.setAttribute(&quot;p&quot;, p);
    %&gt;

    &lt;h2&gt;得到JavaBean的属性值&lt;/h2&gt;
    &lt;%-- 本质上是调用 get方法 --%&gt;
    姓名：${p.name}--&gt;${p.getName()}--&gt;${p[&quot;name&quot;]}， 工资：${p.salary}
    &lt;hr&gt;

    &lt;%
        List&lt;String&gt; names = Arrays.asList(&quot;妲己&quot;, &quot;安琪拉&quot;,&quot;王昭君&quot;);
        // 放在请求域中
        request.setAttribute(&quot;names&quot;,names);
    %&gt;

    &lt;h2&gt;取出集合List中元素&lt;/h2&gt;
    ${names[0]}  --&gt;  ${names[1]}
    &lt;%
        int[] nums = {100,200,300,54};
        // 数组放在请求域
        request.setAttribute(&quot;nums&quot;, nums);
    %&gt;
    &lt;h2&gt;取出数组中元素&lt;/h2&gt;
    ${nums[0]} &amp;nbsp;  ${nums[3]}

    &lt;%
        Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
        map.put(&quot;no1&quot;, &quot;牛魔王&quot;);
        map.put(&quot;no2&quot;, &quot;后羿&quot;);
        map.put(&quot;no3-no4&quot;, &quot;鲁班&quot;);
        //放在请求域
        request.setAttribute(&quot;map&quot;,map);
    %&gt;
    &lt;h2&gt;取出 Map 中元素&lt;/h2&gt;
    &lt;%-- map对象.键名 --%&gt;
    ${map.no1}  --&gt;  ${map.no2} --&gt; ${map[&quot;no3-no4&quot;]}
&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br></div></div><h3 id="_5-3-el-中使用表达式"><a href="#_5-3-el-中使用表达式" class="header-anchor">#</a> 5.3 EL 中使用表达式</h3> <h4 id="_5-3-1-算术表达式"><a href="#_5-3-1-算术表达式" class="header-anchor">#</a> 5.3.1 算术表达式</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/img08.png" alt=""></p> <blockquote><p>Tip：在EL表达式中执行算术运算，只要字符串是数值都会执行算术运行。</p> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code>&lt;%
	request.setAttribute(&quot;a&quot;, &quot;20&quot;);
	request.setAttribute(&quot;b&quot;, &quot;20&quot;);
%&gt;
${a+b}  
${a*b}
${a/b}
${a%b}
${1 + 1} , ${2 - 1}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></blockquote> <h4 id="_5-3-2-比较表达式"><a href="#_5-3-2-比较表达式" class="header-anchor">#</a> 5.3.2 比较表达式</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424163403459.png" alt="image-20200424162328398"></p> <h4 id="_5-3-3-逻辑表达式"><a href="#_5-3-3-逻辑表达式" class="header-anchor">#</a> 5.3.3 逻辑表达式</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424163443490.png" alt=""></p> <h4 id="_5-3-4-三元运算"><a href="#_5-3-4-三元运算" class="header-anchor">#</a> 5.3.4 三元运算</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/img11.png" alt=""></p> <h4 id="_5-3-5-判空表达式"><a href="#_5-3-5-判空表达式" class="header-anchor">#</a> 5.3.5 判空表达式</h4> <p>判空,判断的是内容是否为空,不是对象是否为空.</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/img12.png" alt=""></p> <h4 id="_5-3-6-示例代码-使用上面所有的运算符"><a href="#_5-3-6-示例代码-使用上面所有的运算符" class="header-anchor">#</a> 5.3.6 示例代码：使用上面所有的运算符</h4> <div class="language-html line-numbers-mode"><pre class="language-html"><code>&lt;%@ page import=&quot;java.util.ArrayList&quot; %&gt;
&lt;%@ page import=&quot;java.util.List&quot; %&gt;
&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>算术运算符<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
        ${1+1}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
        ${2-1}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>比较运算符<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
        ${1 eq 1}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
        ${1 != 1}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
        ${1 lt 2}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>逻辑运算符<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
        ${true and false}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
        ${true || false}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
        ${not true}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>三元运算<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
        ${5&gt;6?&quot;男&quot;:&quot;女&quot;}

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>判空表达式<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
        ${empty aaa} <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
        &lt;%
            request.setAttribute(&quot;bbb&quot;,&quot;&quot;);
            // 集合中没有元素
            List<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>String</span><span class="token punctuation">&gt;</span></span> names = new ArrayList&lt;&gt;();
            names.add(&quot;唐僧&quot;);
            request.setAttribute(&quot;ccc&quot;, names);
        %&gt;
        ${empty bbb}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
        ${empty ccc}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h3 id="_5-4-el-从四个作用域中取值-掌握"><a href="#_5-4-el-从四个作用域中取值-掌握" class="header-anchor">#</a> 5.4 EL 从四个作用域中取值（掌握）</h3> <p><strong>EL 获取数据的方式是从 4 个作用域对象中，从小到大的去获取</strong>，如果需要指定作用域获取数据，可使用以下 EL 的内置对象来指定。</p> <h4 id="_5-4-1-pagecontext-对象"><a href="#_5-4-1-pagecontext-对象" class="header-anchor">#</a> 5.4.1 PageContext 对象</h4> <ul><li><p>JSP 本质是 Servlet，但比 Servlet 多了一个作用域：<strong>页面域</strong>，在 JSP 中有四大作用域。</p></li> <li><p>什么是页面域：只在一个JSP页面中起作用，不同的 JSP 之间不能实现数据的共享，比请求域范围还要小。</p></li> <li><p>页面域对象名：pageContext，是 JSP 其中一个内置对象名</p></li></ul> <table><thead><tr><th>PageContext 操作有关的方法</th> <th>说明</th></tr></thead> <tbody><tr><td>void  setAttribute(String key, Object value)</td> <td>向页面域中添加键和值</td></tr> <tr><td>Object  getAttribute(String key)</td> <td>从页面域中得到值</td></tr> <tr><td>void  removeAttribute(String key)</td> <td>删除四个域中同名的键</td></tr> <tr><td>Object findAttribute(String key)</td> <td>自动从四个作用域中去查某个键，<br>从小到大的范围来查找，如果找到就停止。<br>如果没有找到，返回null</td></tr></tbody></table> <p>示例代码：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200514073502317.png" alt="image-20200514073502317"></p> <p><strong>从小到大注释作用域设置数据代码，可看到 EL 表达式在往下获取数据。</strong></p> <h4 id="_5-4-2-el-指定域获取数据"><a href="#_5-4-2-el-指定域获取数据" class="header-anchor">#</a> 5.4.2 EL 指定域获取数据</h4> <table><thead><tr><th>作用域</th> <th>EL的写法</th></tr></thead> <tbody><tr><td>页面域</td> <td>${pageScope.键名}</td></tr> <tr><td>请求域</td> <td>${requestScope.键名}</td></tr> <tr><td>会话域</td> <td>${sessionScope.键名}</td></tr> <tr><td>上下文域</td> <td>${applicationScope.键名}</td></tr></tbody></table> <ul><li>示例代码</li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code>&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>EL指定域获取数据<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
    &lt;%
        // 向页面域中添加一个字符串
        pageContext.setAttribute(&quot;name&quot;, &quot;页面域&quot;);
        // 请求域
        request.setAttribute(&quot;name&quot;, &quot;请求域&quot;);
        // 会话域
        session.setAttribute(&quot;name&quot;, &quot;会话域&quot;);
        // 上下文域
        application.setAttribute(&quot;name&quot;, &quot;上下文域&quot;);
    %&gt;
        
    从页面域获取：${pageScope.name}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    从请求域获取：${requestScope.name}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    从会话域获取：${sessionScope.name}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    从上下文域获取：${applicationScope.name}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    	自动查找作用域获取：${name}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><ul><li>执行效果</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424120432440.png" alt="image-20200424120432440"></p> <h2 id="六、jstl-标签库"><a href="#六、jstl-标签库" class="header-anchor">#</a> 六、JSTL 标签库</h2> <p>JSTL 引入代码</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/emp&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">EmployeeListServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span><span class="token punctuation">{</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 模拟从数据库中去读取到员工列表,真实项目开发,把以下代码换为从数据库中去读取即可</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Employee</span><span class="token punctuation">(</span><span class="token string">&quot;凯哥&quot;</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">BigDecimal</span><span class="token punctuation">(</span><span class="token string">&quot;1000&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Employee</span><span class="token punctuation">(</span><span class="token string">&quot;猴子&quot;</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">BigDecimal</span><span class="token punctuation">(</span><span class="token string">&quot;1000&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Employee</span><span class="token punctuation">(</span><span class="token string">&quot;李白&quot;</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">BigDecimal</span><span class="token punctuation">(</span><span class="token string">&quot;1000&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 把数据放到作用域中</span>
        req<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;list&quot;</span><span class="token punctuation">,</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 跳转到 jsp 中来显示列表</span>
        req<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;/list.jsp&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span>resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><div class="language-jsp line-numbers-mode"><pre class="language-text"><code>    ${list[0].username} -&gt; ${list[0].salary}&lt;br/&gt;
    ${list[1].username} -&gt; ${list[1].salary}&lt;br/&gt;
    ${list[2].username} -&gt; ${list[2].salary}&lt;br/&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>目前使用 EL 只能一个一个元素使用索引去获取,又因为数据库的数据条数是不固定,所以需要使用循环来获取.</p> <p>但是目前只能使用 Java 脚本来做循环,但是在jsp 页面中不要使用Java脚本,所以需要学 新的标签库(JSTL标签库)</p> <h3 id="_6-1-jstl-概述-理解"><a href="#_6-1-jstl-概述-理解" class="header-anchor">#</a> 6.1 JSTL 概述（理解）</h3> <h4 id="_6-1-1-为什么使用-jstl"><a href="#_6-1-1-为什么使用-jstl" class="header-anchor">#</a> 6.1.1 为什么使用 JSTL</h4> <ol><li><p>因为 JSP 页面主要用于显示的，最好不要写 Java 代码 &lt;%%&gt;，如果不用 JSTL 就得写 Java 代码。</p></li> <li><p>对于页面设计人员来说，使用 Java语言操作动态数据是比较困难的，而采用标签和表达式语言相对容易一些， JSTL 的使用为页面设计人员和后台开发人员的分工协作提供了便利。</p></li> <li><p>终极目标：为了简化 JSP 页面的设计。</p></li></ol> <h4 id="_6-1-2-什么是-jstl"><a href="#_6-1-2-什么是-jstl" class="header-anchor">#</a> 6.1.2 什么是 JSTL</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424121334900.png" alt="image-20200424121149515"></p> <ul><li><strong>JSTL 是 JSP 标准标签库，利用标签库的标签可以取代 JSP 页面上的 Java 代码，为页面设计人员和后台开发人员的分工协作提供了便利。</strong></li></ul> <h4 id="_6-1-3-jstl-常用标签库"><a href="#_6-1-3-jstl-常用标签库" class="header-anchor">#</a> 6.1.3 JSTL 常用标签库</h4> <table><thead><tr><th>标签库名</th> <th>URL字符串</th> <th>作用</th> <th>前缀</th></tr></thead> <tbody><tr><td>核心标签库</td> <td><code>http://java.sun.com/jsp/jstl/core</code></td> <td>用于页面逻辑控制<br>如：if、forEach</td> <td>c</td></tr> <tr><td>格式化标签库</td> <td><code>http://java.sun.com/jsp/jstl/fmt</code></td> <td>用于执行格式操作<br>如：格式化日期字符串</td> <td>fmt</td></tr></tbody></table> <h4 id="_6-1-4-jstl-使用步骤"><a href="#_6-1-4-jstl-使用步骤" class="header-anchor">#</a> 6.1.4 JSTL 使用步骤</h4> <ol><li><p>导入 jar 包，每个标签底层其实都是用 Java 代码实现的，复制 jar 包到 <strong>web/WEB-INF/lib</strong> 目录</p> <p>也可以在 tomcat 目录下 webapps\examples\WEB-INF\lib 去获取 JSTL 的两个包</p></li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200424122551449.png" alt="image-20200424122551449"></p> <ol start="2"><li><p><strong>导入 jar (File -&gt; project structure -&gt; Modules) (必须)</strong></p></li> <li><p>创建 JSP 页面，使用 taglib 的指令引用标签库</p></li></ol> <div class="language-html line-numbers-mode"><pre class="language-html"><code>&lt;%--prefix表示前缀，固定为c，uri 标识 --%&gt;
&lt;%@taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ol start="4"><li>在 JSP 页面中就可以使用标签库定义好的标签，比如：<code>&lt;c:if&gt;</code></li></ol> <h3 id="_6-2-核心标签库常用标签"><a href="#_6-2-核心标签库常用标签" class="header-anchor">#</a> 6.2 核心标签库常用标签</h3> <h4 id="_6-2-1-c-if-标签-掌握"><a href="#_6-2-1-c-if-标签-掌握" class="header-anchor">#</a> 6.2.1 <code>&lt;c:if&gt;</code> 标签（掌握）</h4> <ul><li>作用： 用于页面上单条件判断。</li></ul> <table><thead><tr><th>属性名</th> <th>是否支持EL</th> <th>属性类型</th> <th>属 性 描 述</th></tr></thead> <tbody><tr><td>test</td> <td>支持，必须是 EL</td> <td>boolean值</td> <td>EL 中条件为真则执行标签体内容<br>注意：没有对应 else 标签</td></tr></tbody></table> <ul><li>示例：如果用户提交的 age 值大于等于18，则显示你已经成年，否则显示未成年。</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200514074233935.png" alt="image-20200514074233935"></p> <ul><li>代码</li></ul> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code>&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;%--prefix表示前缀，固定为c，uri 标识 --%&gt;
&lt;%@taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;if标签&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;% 
    	// 设置 age 到请求作用域
    	request.SetAttribute(&quot;age&quot;,20) 
    %&gt;
    &lt;c:if test=&quot;${age &gt;=18}&quot;&gt;
        你已经成年
    &lt;/c:if&gt;
    &lt;c:if test=&quot;${age &lt; 18}&quot;&gt;
        未成年，请绕行
    &lt;/c:if&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h4 id="_6-2-2-c-choose-标签-掌握"><a href="#_6-2-2-c-choose-标签-掌握" class="header-anchor">#</a> 6.2.2 <code>&lt;c:choose&gt;</code>标签（掌握）</h4> <ul><li>作用：用于多分支判断(多条件判断)</li></ul> <table><thead><tr><th>标签名</th> <th>作用</th></tr></thead> <tbody><tr><td>choose</td> <td>类似于 java 中swtich,  choose只是一个容器，包含下面两个元素</td></tr> <tr><td>when</td> <td>可以出现多个,<br>用于每个判断条件，类似于 switch 中 case。有一个 test 属性，与 if 的 test 功能相同</td></tr> <tr><td>otherwise</td> <td>如果上面所有的条件都不满足，执行 otherwise 内容。类似于 switch 中 default</td></tr></tbody></table> <ul><li>示例：接收用户提交的分数，根据分数输出等级
<ul><li>80~100优秀</li> <li>60~80及格</li> <li>0~60不及格</li> <li>其它输出分数不正确。</li></ul></li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200514074453741.png" alt="image-20200514074453741"></p> <ul><li>代码</li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code>&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;%@taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>choose标签<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
    &lt;% 
    	// 设置 age 到请求作用域
    	request.SetAttribute(&quot;score&quot;,80) 
    %&gt;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">c:</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>${not empty score}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">c:</span>choose</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">c:</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>${score &gt;=80 &amp;&amp; score&lt;=100}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token value css language-css"><span class="token property">color</span><span class="token punctuation">:</span> red</span><span class="token punctuation">&quot;</span></span></span><span class="token punctuation">&gt;</span></span>优秀<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">c:</span>when</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">c:</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>${score &gt;=60 &amp;&amp; score&lt;80}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token value css language-css"><span class="token property">color</span><span class="token punctuation">:</span> green</span><span class="token punctuation">&quot;</span></span></span><span class="token punctuation">&gt;</span></span>良好<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">c:</span>when</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">c:</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>${score &gt;=0 &amp;&amp; score&lt;60}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token value css language-css"><span class="token property">color</span><span class="token punctuation">:</span> dodgerblue</span><span class="token punctuation">&quot;</span></span></span><span class="token punctuation">&gt;</span></span>不及格<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">c:</span>when</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">c:</span>otherwise</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token value css language-css"><span class="token property">color</span><span class="token punctuation">:</span> orange</span><span class="token punctuation">&quot;</span></span></span><span class="token punctuation">&gt;</span></span>分数不正确<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">c:</span>otherwise</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">c:</span>choose</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">c:</span>if</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><h4 id="_6-2-3-c-foreach-标签-掌握"><a href="#_6-2-3-c-foreach-标签-掌握" class="header-anchor">#</a> 6.2.3 <code>&lt;c:forEach&gt;</code>标签（掌握）</h4> <ul><li>作用：用于遍历集合或数组  for(类型 变量名 : 集合)</li></ul> <table><thead><tr><th>属性名</th> <th>是否支持EL</th> <th>属性类型</th> <th>属性描述</th></tr></thead> <tbody><tr><td>items</td> <td>true</td> <td>数组或集合</td> <td>使用 EL 表达式，代表集合或数组</td></tr> <tr><td>var</td> <td>false</td> <td>String</td> <td>var 的变量名代表集合中的每一个元素</td></tr> <tr><td>varStatus</td> <td>false</td> <td>String</td> <td>代表每个元素的状态对象，<br>一共有4个属性，属性的含义见下表</td></tr></tbody></table> <ul><li>varStatus属性</li></ul> <table><thead><tr><th>属性</th> <th>数据类型</th> <th>含义</th></tr></thead> <tbody><tr><td>index</td> <td>int</td> <td>当前遍历到的这个元素索引号，从 0 开始</td></tr> <tr><td><strong>count</strong></td> <td>int</td> <td>遍历到当前为止，一共遍历了多少个元素,从1 开始</td></tr> <tr><td>first</td> <td>boolean</td> <td>如果当前遍历的是第1个元素，则返回true</td></tr> <tr><td>last</td> <td>boolean</td> <td>如果当前遍历的是最后1个元素，则返回true</td></tr></tbody></table> <ul><li><p>示例：遍历集合</p> <p>学生信息：包含编号，姓名，性别，成绩。在 Servlet 中得到所有的学生信息，把信息放到请求域中，转发到JSP 页面，在 JSP 页面上使用 forEach 标签输出所有的学生信息。</p></li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200514080603278.png" alt="image-20200514080603278"></p> <ul><li>Student 代码</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Getter</span>
<span class="token annotation punctuation">@Setter</span>
<span class="token annotation punctuation">@AllArgsConstructor</span>
<span class="token annotation punctuation">@NoArgsConstructor</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> gender<span class="token punctuation">;</span> <span class="token comment">// true 男 false 女</span>
    <span class="token keyword">private</span> <span class="token keyword">double</span> score<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li>Servlet 代码</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">&quot;/list&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ListStudentServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 创建集合存储学生信息：假装从数据库查询的学生信息</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Student</span><span class="token punctuation">&gt;</span></span> stus <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Student</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stus<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">,</span><span class="token string">&quot;嫦娥&quot;</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">,</span><span class="token number">65</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stus<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token string">&quot;婉儿&quot;</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">,</span><span class="token number">85</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stus<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">3000</span><span class="token punctuation">,</span><span class="token string">&quot;安琪拉&quot;</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">,</span><span class="token number">90</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stus<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">4000</span><span class="token punctuation">,</span><span class="token string">&quot;蜘蛛精&quot;</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">,</span><span class="token number">87</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stus<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">,</span><span class="token string">&quot;孙悟空&quot;</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">,</span><span class="token number">85</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 将集合存储请求域中</span>
        request<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;stus&quot;</span><span class="token punctuation">,</span> stus<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 转发到JSP页面显示数据</span>
        request<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;/list.jsp&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><ul><li>JSP代码</li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code>&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>学生信息表<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
        <span class="token selector">tr</span> <span class="token punctuation">{</span>
            <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>table</span> <span class="token attr-name">align</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>center<span class="token punctuation">&quot;</span></span> <span class="token attr-name">border</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>1<span class="token punctuation">&quot;</span></span> <span class="token attr-name">cellspacing</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>0<span class="token punctuation">&quot;</span></span> <span class="token attr-name">cellpadding</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>0<span class="token punctuation">&quot;</span></span> <span class="token attr-name">width</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>80%<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>caption</span><span class="token punctuation">&gt;</span></span>学生信息列表<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>caption</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>tr</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span><span class="token punctuation">&gt;</span></span>序号<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span><span class="token punctuation">&gt;</span></span>学号<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span><span class="token punctuation">&gt;</span></span>姓名<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span><span class="token punctuation">&gt;</span></span>性别<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span><span class="token punctuation">&gt;</span></span>成绩<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>tr</span><span class="token punctuation">&gt;</span></span>

    &lt;%--
        forEach标签：用于JSP页面遍历集合和数组
            items属性：设置要遍历的集合或数组：一般从作用域中获取
            var属性：设置一个变量名：用来接收遍历到的每一个元素

            varStatus属性：设置一个变量名：记录当前遍历元素的状态(状态对象)
                index 属性：当前遍历元素的在集合中的索引值：从0开始
                count 属性：遍历到当前元素为止已经遍历了多少个元素,从 1 开始
    --%&gt;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">c:</span>forEach</span> <span class="token attr-name">items</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>${stus}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">var</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>stu<span class="token punctuation">&quot;</span></span> <span class="token attr-name">varStatus</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>status<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!-- 给偶数行设置背景颜色 --&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>tr</span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token value css language-css"><span class="token selector">background-color:$</span><span class="token punctuation">{</span>status.count % 2 == 0 ? <span class="token string">'gray;'</span> <span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">}</span></span><span class="token punctuation">&quot;</span></span></span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>td</span><span class="token punctuation">&gt;</span></span>${status.count}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>td</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>td</span><span class="token punctuation">&gt;</span></span>${stu.id}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>td</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>td</span><span class="token punctuation">&gt;</span></span>${stu.name}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>td</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>td</span><span class="token punctuation">&gt;</span></span>${stu.gender? &quot;男&quot;:&quot;女&quot;}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>td</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>td</span><span class="token punctuation">&gt;</span></span>${stu.score}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>td</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>tr</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token namespace">c:</span>forEach</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>table</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br></div></div><h3 id="_6-3-格式化标签库常用标签"><a href="#_6-3-格式化标签库常用标签" class="header-anchor">#</a> 6.3 格式化标签库常用标签</h3> <h4 id="_6-3-1-fmt-formatdate"><a href="#_6-3-1-fmt-formatdate" class="header-anchor">#</a> 6.3.1 <code>&lt;fmt:formatDate&gt;</code></h4> <ul><li>作用：用于对日期进行格式化</li></ul> <table><thead><tr><th>属性名</th> <th>属性类型</th> <th>属 性 描 述</th></tr></thead> <tbody><tr><td>value</td> <td>Date</td> <td>要格式化的日期对象</td></tr> <tr><td>pattern</td> <td>String</td> <td>指定日期格式</td></tr></tbody></table> <ul><li>示例代码</li></ul> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code>&lt;%@ page import=&quot;java.util.Date&quot; %&gt;
&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;%-- 引入标签库 --%&gt;
&lt;%@taglib prefix=&quot;fmt&quot; uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;formatDate标签&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;%
        request.setAttribute(&quot;date&quot;, new Date());
    %&gt;
    当前时间：
    &lt;fmt:formatDate value=&quot;${date}&quot; pattern=&quot;yyyy-MM-dd HH:mm:ss&quot;&gt;&lt;/fmt:formatDate&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="七、小结与作业"><a href="#七、小结与作业" class="header-anchor">#</a> 七、小结与作业</h2> <h3 id="_7-1-小结"><a href="#_7-1-小结" class="header-anchor">#</a> 7.1 小结</h3> <ul><li><p>掌握 jsp hello 程序</p></li> <li><p>了解 jsp 的运行过程</p></li> <li><p>了解 jsp 的语法（可以不用写代码）</p></li> <li><p>理解为什么需要做跳转和数据共享</p></li> <li><p>掌握请求转发和重定向的使用</p></li> <li><p>理解 请求转发和重定向的区别(验证)</p></li> <li><p>掌握 请求转发和重定向的选用问题</p></li> <li><p>理解作用域的作用以及三大作用域的作用范围</p></li> <li><p>掌握三大作用域如何共享数据(API:1  如何获取作用域对象,2 如何通过作用域对象去共享数据)</p></li> <li><p>理解 EL 表达式的作用 : 从四个作用域中从小到大去获取数据</p></li> <li><p>掌握 使用 EL 表达式去获取各种类型的数据</p></li> <li><p>理解 JSTL 标签库的作用</p></li> <li><p>掌握 使用 JSTL 标签库的使用步骤</p></li> <li><p>掌握 JSTL 标签库的常用标签 (条件判断标签,循环标签)</p></li> <li><p>了解 JSTL 标签库的格式化标签(Date 格式化)</p></li> <li></li></ul> <h3 id="_7-2-作业"><a href="#_7-2-作业" class="header-anchor">#</a> 7. 2 作业</h3> <h4 id="_7-2-1-题目1"><a href="#_7-2-1-题目1" class="header-anchor">#</a> 7.2.1 题目1</h4> <p>需求：实现一个登陆页面，提交账号和密码，如果账号为 admin，密码为 root，则返回一个 table表格（user:姓名，年龄，邮箱：2条数据）给浏览器，如果账号密码不为 admin 和 root ，则显示账号或者密码错误到浏览器上。</p> <h4 id="_7-2-2-题目2"><a href="#_7-2-2-题目2" class="header-anchor">#</a> 7.2.2 题目2</h4> <p>需求：完成往数据库中某张表加入数据功能</p> <p>​	表单收集数据 -&gt; Servlet 接受数据封装成对象，调用 dao 的插入方法来加入数据 （<strong>jar 要放到 WEB-INF/lib</strong>）</p> <p>使⽤技术栈（JSP + Servlet + DAO组件 + Mybatis）完成⼀张表的查询操作和新增操作</p> <table><thead><tr><th>id(BIGINT) 主键</th> <th>name(VARCHAR) 姓名</th> <th>age（INT）年龄</th></tr></thead> <tbody><tr><td>10</td> <td>willie</td> <td>18</td></tr> <tr><td>20</td> <td>cun</td> <td>17</td></tr> <tr><td>30</td> <td>存哥</td> <td>19</td></tr></tbody></table></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=JSP" title="标签">#JSP</a><a href="/tags/?tag=JSTL" title="标签">#JSTL</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/9902e4/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">请求与响应</div></a> <a href="/pages/aa67fd/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">WEB版CRUD</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/9902e4/" class="prev">请求与响应</a></span> <span class="next"><a href="/pages/aa67fd/">WEB版CRUD</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/41.2f80f24b.js" defer></script>
  </body>
</html>
